<template>
  <el-card class="box-card" style="margin:20px 0">
    <div slot="header" class="clearfix">
      <!--  @tab-click="handleClick" -->
      <!-- 头部左侧内容 -->
      <el-tabs class="tab" v-model="activeName">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visit"></el-tab-pane>
      </el-tabs>
      <!-- 头部右侧内容 -->
      <div class="header-right">
        <span @click="setDay">今日</span>
        <span @click="setWeek">本周</span>
        <span @click="setMonth">本月</span>
        <span @click="setYear">本年</span>
        <el-date-picker
          v-model="date"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          class="date-picker"
          size="mini"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>
    </div>
    <div>
      <el-row :gutter="10">
        <el-col :span="18">
          <!-- 容器 -->
          <barChart :title="title" :listState="listState"/>
        </el-col>
        <el-col :span="6">
          <div class="sale-right">
            <h3>门店{{title}}额排名</h3>
            <ul v-if="activeName=='sale'">
              <li v-for="(userInfo,index) in listState.userRank" :key="userInfo.index">
                <span class="sale-right-index">{{userInfo.no}}</span>
                <span>{{userInfo.name}}</span>
                <span class="sale-right-value">{{userInfo.money}}</span>
              </li>
            </ul>
            <ul v-else>
              <li v-for="(orderInfo,index) in listState.orderRank" :key="orderInfo.index">
                <span class="sale-right-index">{{orderInfo.no}}</span>
                <span>{{orderInfo.name}}</span>
                <span class="sale-right-value">{{orderInfo.money}}</span>
              </li>
            </ul>
          </div>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
import barChart from './barChart'
import dayjs from 'dayjs'
import { mapState } from 'vuex'
export default {
  components: { barChart },
  data() {
    return {
      activeName: 'sale',
      // 收集日历数据
      date: [],
    }
  },
  computed: {
    // 计算属性：标题
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: (state) => state.home.list,
    }),
  },
  methods: {
    // 今日
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    // 本周
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本月
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    // 本年
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    },
  },
}
</script>

<style scoped>
>>> .el-card__header {
  border-bottom: none;
}
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.header-right {
  position: absolute;
  right: 0;
}
.header-right span,
.sale-right span {
  margin: 0 20px;
}
.date-picker {
  width: 220px;
  margin: 0 10px;
}
.sale-right {
  padding: 0;
}
ul {
  list-style: none;
  width: 100%;
  height: 200px;
  padding: 0;
}
ul li {
  height: 8%;
  margin: 20px 0;
}
.sale-right-index {
  width: 1.25rem;
  height: 1.25rem;
  line-height: 1.25rem;
  border-radius: 50%;
  color: white;
  background-color: #000;
  text-align: center;
  float: left;
}
.sale-right-value {
  float: right;
}
</style>